package com.wolf.controller;

import com.wolf.pojo.Wl_user_competence;
import com.wolf.pojo.Wl_user_role_competences;
import com.wolf.service.Wl_user_competenceService;
import com.wolf.util.Math;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@ComponentScan
public class Wl_user_competenceController {

    @Autowired
    private Wl_user_competenceService wl_user_competenceService;

    public void setWl_user_competenceService(Wl_user_competenceService wl_user_competenceService) {
        this.wl_user_competenceService = wl_user_competenceService;
    }

    @RequestMapping("wl_user_competence_getall")
    public String getAll(ModelMap modelMap, HttpServletRequest request){

        List<Wl_user_competence> list = wl_user_competenceService.getAll();

        List<Wl_user_role_competences> lists = (List<Wl_user_role_competences>) request.getSession().getAttribute("wl_user_role_competences_list");

        for(Wl_user_competence wc:list){
            for(Wl_user_role_competences ws:lists){
                if(ws.getWl_user_competence().getCompetencename().equals(wc.getCompetencename())){
                    wc.setIndex(1);
                }
            }
        }

        modelMap.addAttribute("wl_user_role_competences_list",lists);

        modelMap.addAttribute("wl_user_competence_list",list);

        modelMap.addAttribute("wl_user_role_bean",request.getSession().getAttribute("wl_user_role_bean"));


        return "table_wl_user_role_competences";
    }

    @RequestMapping("wl_user_competence_getalls")
    public String getalls(ModelMap modelMap){

        List<Wl_user_competence> list = wl_user_competenceService.getAll();

        modelMap.addAttribute("wl_user_competence_lists",list);

        return"table_wl_user_competence";
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("wl_user_competence_delete")
    public String del(String id){

        wl_user_competenceService.deleteByPrimaryKey(id);

        return "redirect:wl_user_competence_getalls";
    }

    /**
     * 模糊查询
     * @param name
     * @param code
     * @return
     */
    @RequestMapping("wl_user_competence_getselective")
    public String wl_user_competence_getselective(ModelMap modelMap,String name,String code){

        List<Wl_user_competence> list = wl_user_competenceService.getSelective(name,code);

        modelMap.addAttribute("wl_user_competence_lists",list);

        return"table_wl_user_competence";
    }

    /**
     * 新增
     * @param wl_user_competence
     * @return
     */
    @RequestMapping("wl_user_competence_insert")
    public String wl_user_competence_insert(Wl_user_competence wl_user_competence){
        wl_user_competence.setId(Math.random());
        wl_user_competenceService.insert(wl_user_competence);

        return "redirect:wl_user_competence_getalls";
    }

    /**
     * 查询单个对象
     * @param id
     * @return
     */
    @RequestMapping("wl_user_competence_getbyid")
    @ResponseBody
    public Wl_user_competence wl_user_competence_getbyid(String id){
        return wl_user_competenceService.selectByPrimaryKey(id);
    }

    /**
     * 修改
     * @param id
     * @return
     */
    @RequestMapping("wl_user_competence_update")
    @ResponseBody
    public int wl_user_competence_update(String id,String names,String codes,String urls){

        Wl_user_competence wl_user_competence = new Wl_user_competence();
        wl_user_competence.setId(id);
        wl_user_competence.setCompetencename(names);
        wl_user_competence.setCompetencecode(codes);
        wl_user_competence.setUrl(urls);

        return wl_user_competenceService.updateByPrimaryKeySelective(wl_user_competence);

    }

}
